package cn.line5.seam.cms.article;

import java.util.List;

import cn.line5.seam.annotations.Name;
import cn.line5.seam.db.PageResult;
import cn.line5.seam.db.Query;
import cn.line5.seam.db.QueryFactory;

@Name("articleDao")
/**
 * <h1>LSEAM框架示例数据库实现类</h1>
 * <br/>
 * 
 * @author Liu,yandong
 */
public class ArticleDaoImpl implements ArticleDao {
	public List<ArticleVo> getList() {
		Query query = QueryFactory
				.createQuery("select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user");
		List<ArticleVo> l = query.list(ArticleVo.class);
		return l;
	}

	public PageResult<ArticleVo> getPageList(String currentPage) {
		Integer page = Integer.parseInt(currentPage == null
				|| currentPage.length() <= 0 ? "1" : currentPage);
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user")
				.setCurrentPage(
						page > 0 ? page : 1).setPageRows(3);
		PageResult<ArticleVo> pr = query.pageList(ArticleVo.class);
		return pr;
	}

	public ArticleVo getArticle(String id) {
		Query query = QueryFactory
				.createQuery(
						"select user_id as userId, login_name as loginName, login_pass as loginPass, user_name as userName, user_photo as userPhoto from t_user where user_id=?")
				.setParameter(1, id);
		return query.single(ArticleVo.class);
	}

	public ArticleVo loginArticle(String loginAccount,String loginPassword){
		String sql = " select "
					+" article_id as articleId, "								//'主键',
					+" article_name as articleName, "							//'学生姓名',
					+" article_login_account as articleLoginAccount, "			//'登陆账号'
					+" article_login_password as articleLoginPassword, "		//'登陆密码'
					+" article_age as articleAge, " 							//'学生年龄'
					+" article_parent_name as articleParentName, "				//'家长姓名'
					+" article_parent_call as articleParentCall, "				//'家长电话'
					+" article_parent_mail as articleParentMail, "				//'家长邮箱'
					+" article_home_address as articleHomeAddress, "			//'家庭住址'
					+" article_introduction as articleIntroduction, "			//'学生情况说明'
					+" article_confirmation as articleConfirmation, "			//'验证方法'
					+" article_registration_date as articleRegistrationDate, "	//'注册时间'
					+" article_test_time as articleTestTime, "					//'预约测试时间'
					+" article_last_login_time as articleLastLoginTime, "		//'最后登录时间'
					+" article_state as articleState "							//'学生状态'
					+" from miko_article "
					+" where 1=1 ";
		if(loginAccount != null && !loginAccount.equals("")){
				sql+=" and article_login_account='"+loginAccount+"' ";
		}
		if(loginPassword != null && !loginPassword.equals("")){
				sql+=" and article_login_password='"+loginPassword+"' ";
		}
		
		Query query = QueryFactory.createQuery(sql);
		
		return query.single(ArticleVo.class);
	}
	
	public ArticleVo getArticleVo(ArticleVo articleVo) {
		Query query = QueryFactory.createQuery(
				 			 " select "
							+" article_id as articleId, "								//'主键',
							+" article_name as articleName, "							//'学生姓名',
							+" article_login_account as articleLoginAccount, "			//'登陆账号'
							+" article_login_password as articleLoginPassword, "		//'登陆密码'
							+" article_age as articleAge, " 							//'学生年龄'
							+" article_parent_name as articleParentName, "				//'家长姓名'
							+" article_parent_call as articleParentCall, "				//'家长电话'
							+" article_parent_mail as articleParentMail, "				//'家长邮箱'
							+" article_home_address as articleHomeAddress, "			//'家庭住址'
							+" article_introduction as articleIntroduction, "			//'学生情况说明'
							+" article_confirmation as articleConfirmation, "			//'验证方法'
							+" article_registration_date as articleRegistrationDate, "	//'注册时间'
							+" article_test_time as articleTestTime, "					//'预约测试时间'
							+" article_last_login_time as articleLastLoginTime, "		//'最后登录时间'
							+" article_state as articleState "							//'学生状态'
							+" from miko_article "
							+" where article_name=? "
							+" and article_parent_name=? "
							+" and article_parent_call=? " )
							.setParameter(1, articleVo.getArticleId());
		
		return query.single(ArticleVo.class);
	}

	public boolean insertArticle(ArticleVo vo) {
		Query query = QueryFactory.createExecute("insert into miko_article "
					+" values( "
					+" :articleId, "					//'主键',
					+" :articleName, "					//'学生姓名',
					+" :articleLoginAccount, "			//'登陆账号'
					+" :articleLoginPassword, "			//'登陆密码'
					+" :articleAge, " 					//'学生年龄'
					+" :articleParentName, "			//'家长姓名'
					+" :articleParentCall, "			//'家长电话'
					+" :articleParentMail, "			//'家长邮箱'
					+" :articleHomeAddress, "			//'家庭住址'
					+" :articleIntroduction, "			//'学生情况说明'
					+" :articleConfirmation, "			//'验证方法'
					+" :articleRegistrationDate, "		//'注册时间'
					+" :articleTestTime, "				//'预约测试时间'
					+" :articleLastLoginTime, "			//'最后登录时间'
					+" :articleState) ",				//'学生状态'
						vo);		
		
		return query.execute() > 0;
	}
	
	public boolean modifyArticle(ArticleVo vo) {
		Query query = QueryFactory
				.createExecute(
						"update t_user set login_name=:loginName, login_pass=:loginPass, user_name=:userName, user_photo=:userPhoto where user_id=:userId",
						vo);
		return query.execute() > 0;
	}

	public boolean deleteArticle(String id) {
		Query query = QueryFactory.createExecute(
				"delete from t_user where user_id=?").setParameter(1, id);
		return query.execute() > 0;
	}
}
